<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas画半圆</title>
<style>
    canvas {
        border: 1px solid black;
    }
</style>
</head>
<body>

<canvas id="canvas" width="1000" height="1000"></canvas>

<script>


// 判断三个点是否共线
function arePointsCollinear(point1, point2, point3) {
    // Convert points to vectors
    let vector1 = { x: point2[0] - point1[0], y: point2[1] - point1[1] };
    let vector2 = { x: point3[0] - point1[0], y: point3[1] - point1[1] };

    // Check if the cross product of the vectors is zero (indicating collinearity)
    return vector1.x * vector2.y - vector1.y * vector2.x === 0;
}
function adjustPointsToBeCollinear(center, point1, point2) {
    // 计算中心点与两个点的向量
    let vector1 = { x: point1[0] - center[0], y: point1[1] - center[1] };
    let vector2 = { x: point2[0] - center[0], y: point2[1] - center[1] };

    // 如果两个向量不共线，则调整第一个点，使其与中心点共线
    if (!arePointsCollinear(center, point1, point2)) {
        // 计算两个向量的长度
        let length1 = Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y);
        let length2 = Math.sqrt(vector2.x * vector2.x + vector2.y * vector2.y);

        // 计算两个向量的方向
        let angle1 = Math.atan2(vector1.y, vector1.x);
        let angle2 = Math.atan2(vector2.y, vector2.x);

        // 计算新的第一个点的位置，使其与中心点共线
        point1[0] = center[0] + Math.cos(angle2) * length1;
        point1[1] = center[1] + Math.sin(angle2) * length1;
    }

    return [point1, point2];
}
// 封装函数，接受三个点和圆心坐标，返回调整后的三个点的坐标信息
function adjustPointsToBeCollinearWithCenter(center, point1, point2) {
    // 调整点使其共线
    let adjustedPoints
    if(!arePointsCollinear(center, point1, point2)) {
      adjustedPoints = adjustPointsToBeCollinear(center, point1, point2);
      return adjustedPoints;
    }
   

    return "共线";
}

// Example usage
let center = [280, 320];
let point1 = [184, 49];
let point2 = [354, 530];

let adjustedPoints = adjustPointsToBeCollinearWithCenter(center, point1, point2);
console.log(adjustedPoints); // 输出调整后的点的坐标信息
console.log(arePointsCollinear(adjustedPoints[0], point2[1], center));
</script>

</body>
</html>
